//
//  speech.m
//  dsBridge
//
//  Created by sxh on 2020/9/18.
//

#import "speech.h"
#import "SpeechSelector.h"
#import "JSBUtil.h"

@implementation speech

- (void)startRecognize:(id)obj :(JSCallBack)completeHandle {
    NSString *timeout = ((NSDictionary *)obj)[@"timeout"];
    if (timeout == nil) {
        timeout = @"1000";
    }
    
    [[SpeechSelector sharedInstace] startRecognizer:^(id  _Nullable result, NSError * _Nullable error) {
        NSLog(@"%@",result);
        NSMutableDictionary *resultDict = @{}.mutableCopy;
        if (error == nil) {
            resultDict = [self setDict:@"200" message:@"识别成功" data:@{
                @"recognizerResult": result
            }];
        }else {
            resultDict = [self setDict:[NSString stringWithFormat:@"%ld",error.code] message:error.userInfo[@"errMessage"] data:@{}];
        }
        completeHandle([JSBUtil objToJsonString:resultDict],YES);
    }];
}

- (void)stopRecognize:(id)obj :(JSCallBack)completeHandle {
    
    [[SpeechSelector sharedInstace] stopRecognizer];
    NSMutableDictionary *resultDict = [self setDict:@"200" message:@"关闭识别" data:@{}];
    completeHandle([JSBUtil objToJsonString:resultDict],YES);
}

#pragma mark - setter
///处理返回数据
- (NSMutableDictionary *)setDict:(NSString *)code
                         message:(NSString *)message
                            data:(id)data{
    return @{
        @"code": code,
        @"message": message,
        @"data": data
    }.mutableCopy;
}
@end
